package com.woniuxy.repair.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.common.dto.OwnerDto;
import com.woniuxy.common.dto.WcnUserDto;
import com.woniuxy.common.utils.ResponseUtil;
import com.woniuxy.repair.feign.UserFeign;
import com.woniuxy.repair.pojo.RepairOrder;
import com.woniuxy.repair.mapper.RepairOrderMapper;
import com.woniuxy.repair.service.RepairOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

import static com.woniuxy.common.utils.UserUtil.*;

/**
* @author 76540
* @description 针对表【repair_order(报修订单表)】的数据库操作Service实现
* @createDate 2025-07-18 14:40:09
*/
@Service
public class RepairOrderServiceImpl extends ServiceImpl<RepairOrderMapper, RepairOrder>
    implements RepairOrderService{
    @Autowired
    private UserFeign userFeign;
    @Autowired
    private HttpServletRequest request;


    @Override
    public RepairOrder addRepair(RepairOrder repairOrder) {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        // 从Authorization头中获取Token
        String token = request.getHeader("Authorization");
        OwnerDto user = getUser1(token);
//        Long id = user.getId();
//        OwnerDto ownerDto = ownerFeign.queryOwnerById(user.getId());
        RepairOrder repairOrder1 = new RepairOrder();
        // 生成订单号（临时，后续会被processInstanceId替换）
//        repairOrder1.setOrderNo("TEMP_" + System.currentTimeMillis());
        repairOrder1.setOwnerId(user.getId());
        repairOrder1.setHouseId(Long.valueOf(user.getRoom()));
        repairOrder1.setRepairTypeId(repairOrder.getRepairTypeId());
        repairOrder1.setDescription(repairOrder.getDescription());
        repairOrder1.setAppointmentTime(repairOrder.getAppointmentTime());
        repairOrder1.setPriority(repairOrder.getPriority());
        repairOrder1.setHandlerId(2L);


        repairOrder1.setBuildingId(user.getBuildingId());
        repairOrder1.setCommunityId(user.getCommunityId());


        save(repairOrder1);
        return repairOrder1;

    }

    @Override
    public void approveRepair(Long repairOrderId, Boolean approved, String comment) {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        // 从Authorization头中获取Token
        String token = request.getHeader("Authorization");
        RepairOrder repairOrder = getById(repairOrderId);
        if (repairOrder == null) {
            throw new RuntimeException("报修单不存在");
        }
        
        if (approved) {
            repairOrder.setStatus(2); // 已受理
        } else {
            repairOrder.setStatus(7); // 已驳回
            repairOrder.setCancelReason(comment);
        }
        
        repairOrder.setUpdateTime(new Date());
        updateById(repairOrder);
    }

    @Override
    public void handleRepair(Long repairOrderId, Boolean completed, String feedback, String repairResult) {

        RepairOrder repairOrder = getById(repairOrderId);
        if (repairOrder == null) {
            throw new RuntimeException("报修单不存在");
        }
        
        if (completed) {
            repairOrder.setStatus(5); // 待验收
            repairOrder.setCompleteTime(new Date());
        } else {
            repairOrder.setStatus(7);
            repairOrder.setCancelReason(repairResult);
        }
        
        repairOrder.setUpdateTime(new Date());
        updateById(repairOrder);
    }

    @Override
    public List<RepairOrder> property() {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        // 从Authorization头中获取Token
        String token = request.getHeader("Authorization");
        WcnUserDto wcnUserDto1 = getUser2(token);
//        WcnUserDto wcnUserDto1 = getUser(request);
        WcnUserDto wcnUserDto = userFeign.queryUserById(wcnUserDto1.getId());

        if (wcnUserDto.getRoleId() == 2) {
            return lambdaQuery()
                    .eq(RepairOrder::getStatus, 1)
                    .list();
        }else if (wcnUserDto.getRoleId() == 3) {
            return lambdaQuery()
                    .eq(RepairOrder::getStatus, 2)
                    .list();
        }
        return null;
    }
}




